-- classes
--
-- Inheritance:
---------------
--
--          Gameelement
--              / \
--             /   \
--            /     \    
--        Skill    Character 
--                     /\          (many more)
--                 Hero  Enemy    /
--                            \-----bear
--                            |   \    
--                      redfish    greenfish
--       Weapon
-- (Container for skills)
-- == global values

directions = {'north', 'northeast', 'east', 'southeast', 'south', 'southwest', 'west', 'northwest'}

-- every gameelem is added here with Gameelement:New()
gameelements = {} 

-- NOTE : the values of the table 'gameelements' should be weak. That means: if there is no other reference to this value (from other table), it will be removed by next garbage collection.

-- collection for all enemies
enemies = {} -- setmetatable(enemies, {__mode = "v" })

--globalfunctions
dofile('globalfunc.lua')
dofile('States.lua')
-- classes !
dofile('gameelement.lua')
dofile('projectile.lua')
dofile('monster.lua')
dofile('graphical_element.lua')
dofile('itemsystem.lua')
-- menu stuff TODO: todo?
dofile('menu.lua')
--dofile('dialog.lua')

-- engine debug / ds
-- 1 = CONSOLEDEBUG 
-- 2 = DS
ENGINE = 2 

-- ==== DS things 

if ENGINE == 2 then
  dofile('engine_ds.lua')
end
-- ==== CONTROLS

controls = {}
controls['button1'] = '' --attack
controls['button2'] = '' --arrow
controls['button3'] = '' --fireball
controls['button4'] = ''
controls['button5'] = ''
controls['button6'] = ''
controls['left'] = ''    --movement
controls['right'] = ''   --movement
controls['up'] = ''      --movement 
controls['down'] = ''   --movement
controls['start'] = ''   --movement
controls['select'] = ''   --movement

-- ================= MAIN File 

game = {}
game.start = {}
game.start.round = {}

game.status = 1
 
if ENGINE == 2 then
-- TODO: dont load things...
--hero.load()
end
-- -- -- -- -- -- -- -- -- --
-- -- Rounds -- -- -- -- -- ======> only for testing the game. Will be deleted.
-- -- -- -- -- -- -- -- -- --

-- sprites / images are loaded at gamestart
paintable_objects = {Fireball, Enemy, Fish, redfish, greenfish, Hero, Projectile, Arrow, Lightning}

-- -- -- -- -- -- -- -- -- --
-- -- hero -- -- -- -- --
-- -- -- -- -- -- -- -- -- --

hero = Hero:new{hp = 1000, x = 0, y = 0, wid = 24, hig = 32, movementSpeed = 1, image = {sprite = 'resources/greenfish.png'}} 
table.insert(gameelements, hero)
hero.currentState = States.control.running


-------Moster
for i= 1, 1 do	
	table.insert(gameelements, redfish:new())
end

if ENGINE == 2 then 
	imageload_ds()
 	controlsupdate_ds()

-- TODO: do this in engine_ds !!!!
	local background = Image.load("resources/background.png", VRAM)

-- -- -- -- -- -- -- -- -- --
-- -- MAINLOOP -- -- -- -- --
-- -- -- -- -- -- -- -- -- --

	while not false do --controls.start == 'pressed' do
		-- TODO: 'run' is 1.runcurrentstate 2. move 3. paint 4. paint ~'oht' 5. changestate if available

		controlsupdate_ds() -- read controlls
	  	startDrawing()
		clock.run()
		if game.status == 1  then -- game running
			paint_ds_layer1() --backgroun

			---------------gameelements are used
			   for k,v  in ipairs(gameelements) do
					v:runState()
					paint_ds(v)
				end
				
			paint_ds_layer3()
		elseif game.status == 2 then --something else   
		end
		stopDrawing()
	end



background = nil
spimg = nil

if ENGINE == 2 then hero.savegame() end
end
